package com.github.bobjoy.java.aop.helper;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class PersonHelper2 {

    private long start;

    public PersonHelper2() {
    }

    @Pointcut("execution(* *.sleep())")
    public void sleeppoint() {
    }

    @Pointcut("@annotation(com.bfl.learning.aop.annotation.Timer)")
    public void timer() {
    }

    @Before("timer()")
    public void before() {
        start = System.currentTimeMillis();
    }

    @Before("sleeppoint()")
    public void beforeSleep() {
        System.out.println("睡觉前要脱衣服!");
    }

    @AfterReturning("sleeppoint()")
    public void afterSleep() {
        System.out.println("睡醒了要穿衣服！");
    }

    @After("timer()")
    public void after() {
        long end = System.currentTimeMillis();
        System.out.println("耗时：" + (end - start));
    }

    //当抛出异常时被调用
    public void doThrowing(JoinPoint point, Throwable ex) {
        System.out.println("doThrowing::method "
                + point.getTarget().getClass().getName() + "."
                + point.getSignature().getName() + " throw exception");
        System.out.println(ex.getMessage());
    }

}
